<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>browserrange_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_dom_browserrange_browserrange_test.js.html">browserrange_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.dom.browserrangeTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.dom.browserrangeTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line20"></a>goog.require(&#39;goog.dom.Range&#39;);
<a name="line21"></a>goog.require(&#39;goog.dom.RangeEndpoint&#39;);
<a name="line22"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line23"></a>goog.require(&#39;goog.dom.browserrange&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.dom&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line26"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line27"></a>
<a name="line28"></a>var test1;
<a name="line29"></a>var test2;
<a name="line30"></a>var cetest;
<a name="line31"></a>var empty;
<a name="line32"></a>var dynamic;
<a name="line33"></a>var onlybrdiv;
<a name="line34"></a>
<a name="line35"></a>function setUpPage() {
<a name="line36"></a>  test1 = goog.dom.getElement(&#39;test1&#39;);
<a name="line37"></a>  test2 = goog.dom.getElement(&#39;test2&#39;);
<a name="line38"></a>  cetest = goog.dom.getElement(&#39;cetest&#39;);
<a name="line39"></a>  empty = goog.dom.getElement(&#39;empty&#39;);
<a name="line40"></a>  dynamic = goog.dom.getElement(&#39;dynamic&#39;);
<a name="line41"></a>  onlybrdiv = goog.dom.getElement(&#39;onlybr&#39;);
<a name="line42"></a>}
<a name="line43"></a>
<a name="line44"></a>function testCreate() {
<a name="line45"></a>  assertNotNull(&#39;Browser range object can be created for node&#39;,
<a name="line46"></a>      goog.dom.browserrange.createRangeFromNodeContents(test1));
<a name="line47"></a>}
<a name="line48"></a>
<a name="line49"></a>function testRangeEndPoints() {
<a name="line50"></a>  var container = cetest.firstChild;
<a name="line51"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line52"></a>      container, 2, container, 2);
<a name="line53"></a>  range.select();
<a name="line54"></a>
<a name="line55"></a>  var selRange = goog.dom.Range.createFromWindow();
<a name="line56"></a>  var startNode = selRange.getStartNode();
<a name="line57"></a>  var endNode = selRange.getEndNode();
<a name="line58"></a>  var startOffset = selRange.getStartOffset();
<a name="line59"></a>  var endOffset = selRange.getEndOffset();
<a name="line60"></a>  if (goog.userAgent.WEBKIT) {
<a name="line61"></a>    assertEquals(&#39;Start node should have text: abc&#39;,
<a name="line62"></a>        &#39;abc&#39;, startNode.nodeValue);
<a name="line63"></a>    assertEquals(&#39;End node should have text: abc&#39;, &#39;abc&#39;, endNode.nodeValue);
<a name="line64"></a>    assertEquals(&#39;Start offset should be 3&#39;, 3, startOffset);
<a name="line65"></a>    assertEquals(&#39;End offset should be 3&#39;, 3, endOffset);
<a name="line66"></a>  } else {
<a name="line67"></a>    assertEquals(&#39;Start node should be the first div&#39;, container, startNode);
<a name="line68"></a>    assertEquals(&#39;End node should be the first div&#39;, container, endNode);
<a name="line69"></a>    assertEquals(&#39;Start offset should be 2&#39;, 2, startOffset);
<a name="line70"></a>    assertEquals(&#39;End offset should be 2&#39;, 2, endOffset);
<a name="line71"></a>  }
<a name="line72"></a>}
<a name="line73"></a>
<a name="line74"></a>function testCreateFromNodeContents() {
<a name="line75"></a>  var range = goog.dom.Range.createFromNodeContents(onlybrdiv);
<a name="line76"></a>  goog.testing.dom.assertRangeEquals(onlybrdiv, 0, onlybrdiv, 1, range);
<a name="line77"></a>}
<a name="line78"></a>
<a name="line79"></a>function normalizeHtml(str) {
<a name="line80"></a>  return str.toLowerCase().replace(/[\n\r\f&quot;]/g, &#39;&#39;);
<a name="line81"></a>}
<a name="line82"></a>
<a name="line83"></a>// TODO(robbyw): We really need tests for (and code fixes for)
<a name="line84"></a>// createRangeFromNodes in the following cases:
<a name="line85"></a>// * BR boundary (before + after)
<a name="line86"></a>
<a name="line87"></a>function testCreateFromNodes() {
<a name="line88"></a>  var start = test1.firstChild;
<a name="line89"></a>  var range = goog.dom.browserrange.createRangeFromNodes(start, 2,
<a name="line90"></a>      test2.firstChild, 2);
<a name="line91"></a>  assertNotNull(&#39;Browser range object can be created for W3C node range&#39;,
<a name="line92"></a>      range);
<a name="line93"></a>
<a name="line94"></a>  assertEquals(&#39;Start node should be selected at start endpoint&#39;, start,
<a name="line95"></a>      range.getStartNode());
<a name="line96"></a>  assertEquals(&#39;Selection should start at offset 2&#39;, 2,
<a name="line97"></a>      range.getStartOffset());
<a name="line98"></a>
<a name="line99"></a>  assertEquals(&#39;Text node should be selected at end endpoint&#39;,
<a name="line100"></a>      test2.firstChild, range.getEndNode());
<a name="line101"></a>  assertEquals(&#39;Selection should end at offset 2&#39;, 2, range.getEndOffset());
<a name="line102"></a>
<a name="line103"></a>  assertTrue(&#39;Text content should be &quot;xt\\s*ab&quot;&#39;,
<a name="line104"></a>      /xt\s*ab/.test(range.getText()));
<a name="line105"></a>  assertFalse(&#39;Nodes range is not collapsed&#39;, range.isCollapsed());
<a name="line106"></a>  assertEquals(&#39;Should contain correct html fragment&#39;,
<a name="line107"></a>      &#39;xt&lt;/div&gt;&lt;div id=test2&gt;ab&#39;,
<a name="line108"></a>      normalizeHtml(range.getHtmlFragment()));
<a name="line109"></a>  assertEquals(&#39;Should contain correct valid html&#39;,
<a name="line110"></a>      &#39;&lt;div id=test1&gt;xt&lt;/div&gt;&lt;div id=test2&gt;ab&lt;/div&gt;&#39;,
<a name="line111"></a>      normalizeHtml(range.getValidHtml()));
<a name="line112"></a>}
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>function testTextNode() {
<a name="line116"></a>  var range = goog.dom.browserrange.createRangeFromNodeContents(
<a name="line117"></a>      test1.firstChild);
<a name="line118"></a>
<a name="line119"></a>  assertEquals(&#39;Text node should be selected at start endpoint&#39;, &#39;Text&#39;,
<a name="line120"></a>      range.getStartNode().nodeValue);
<a name="line121"></a>  assertEquals(&#39;Selection should start at offset 0&#39;, 0,
<a name="line122"></a>      range.getStartOffset());
<a name="line123"></a>
<a name="line124"></a>  assertEquals(&#39;Text node should be selected at end endpoint&#39;, &#39;Text&#39;,
<a name="line125"></a>      range.getEndNode().nodeValue);
<a name="line126"></a>  assertEquals(&#39;Selection should end at offset 4&#39;, &#39;Text&#39;.length,
<a name="line127"></a>      range.getEndOffset());
<a name="line128"></a>
<a name="line129"></a>  assertEquals(&#39;Container should be text node&#39;, goog.dom.NodeType.TEXT,
<a name="line130"></a>      range.getContainer().nodeType);
<a name="line131"></a>
<a name="line132"></a>  assertEquals(&#39;Text content should be &quot;Text&quot;&#39;, &#39;Text&#39;, range.getText());
<a name="line133"></a>  assertFalse(&#39;Text range is not collapsed&#39;, range.isCollapsed());
<a name="line134"></a>  assertEquals(&#39;Should contain correct html fragment&#39;, &#39;Text&#39;,
<a name="line135"></a>      range.getHtmlFragment());
<a name="line136"></a>  assertEquals(&#39;Should contain correct valid html&#39;,
<a name="line137"></a>      &#39;Text&#39;, range.getValidHtml());
<a name="line138"></a>
<a name="line139"></a>}
<a name="line140"></a>
<a name="line141"></a>function testTextNodes() {
<a name="line142"></a>  goog.dom.removeChildren(dynamic);
<a name="line143"></a>  dynamic.appendChild(goog.dom.createTextNode(&#39;Part1&#39;));
<a name="line144"></a>  dynamic.appendChild(goog.dom.createTextNode(&#39;Part2&#39;));
<a name="line145"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line146"></a>      dynamic.firstChild, 0, dynamic.lastChild, 5);
<a name="line147"></a>
<a name="line148"></a>  assertEquals(&#39;Text node 1 should be selected at start endpoint&#39;, &#39;Part1&#39;,
<a name="line149"></a>      range.getStartNode().nodeValue);
<a name="line150"></a>  assertEquals(&#39;Selection should start at offset 0&#39;, 0,
<a name="line151"></a>      range.getStartOffset());
<a name="line152"></a>
<a name="line153"></a>  assertEquals(&#39;Text node 2 should be selected at end endpoint&#39;, &#39;Part2&#39;,
<a name="line154"></a>      range.getEndNode().nodeValue);
<a name="line155"></a>  assertEquals(&#39;Selection should end at offset 5&#39;, &#39;Part2&#39;.length,
<a name="line156"></a>      range.getEndOffset());
<a name="line157"></a>
<a name="line158"></a>  assertEquals(&#39;Container should be DIV&#39;, goog.dom.TagName.DIV,
<a name="line159"></a>      range.getContainer().tagName);
<a name="line160"></a>
<a name="line161"></a>  assertEquals(&#39;Text content should be &quot;Part1Part2&quot;&#39;, &#39;Part1Part2&#39;,
<a name="line162"></a>      range.getText());
<a name="line163"></a>  assertFalse(&#39;Text range is not collapsed&#39;, range.isCollapsed());
<a name="line164"></a>  assertEquals(&#39;Should contain correct html fragment&#39;, &#39;Part1Part2&#39;,
<a name="line165"></a>      range.getHtmlFragment());
<a name="line166"></a>  assertEquals(&#39;Should contain correct valid html&#39;,
<a name="line167"></a>      &#39;part1part2&#39;,
<a name="line168"></a>      normalizeHtml(range.getValidHtml()));
<a name="line169"></a>
<a name="line170"></a>}
<a name="line171"></a>
<a name="line172"></a>function testDiv() {
<a name="line173"></a>  var range = goog.dom.browserrange.createRangeFromNodeContents(test2);
<a name="line174"></a>
<a name="line175"></a>  assertEquals(&#39;Text node &quot;abc&quot; should be selected at start endpoint&#39;, &#39;abc&#39;,
<a name="line176"></a>      range.getStartNode().nodeValue);
<a name="line177"></a>  assertEquals(&#39;Selection should start at offset 0&#39;, 0,
<a name="line178"></a>      range.getStartOffset());
<a name="line179"></a>
<a name="line180"></a>  assertEquals(&#39;Text node &quot;def&quot; should be selected at end endpoint&#39;, &#39;def&#39;,
<a name="line181"></a>      range.getEndNode().nodeValue);
<a name="line182"></a>  assertEquals(&#39;Selection should end at offset 3&#39;, &#39;def&#39;.length,
<a name="line183"></a>      range.getEndOffset());
<a name="line184"></a>
<a name="line185"></a>  assertEquals(&#39;Container should be DIV&#39;, &#39;DIV&#39;,
<a name="line186"></a>      range.getContainer().tagName);
<a name="line187"></a>
<a name="line188"></a>  assertTrue(&#39;Div text content should be &quot;abc\\s*def&quot;&#39;,
<a name="line189"></a>      /abc\s*def/.test(range.getText()));
<a name="line190"></a>  assertEquals(&#39;Should contain correct html fragment&#39;, &#39;abc&lt;br id=br&gt;def&#39;,
<a name="line191"></a>      normalizeHtml(range.getHtmlFragment()));
<a name="line192"></a>  assertEquals(&#39;Should contain correct valid html&#39;,
<a name="line193"></a>      &#39;&lt;div id=test2&gt;abc&lt;br id=br&gt;def&lt;/div&gt;&#39;,
<a name="line194"></a>      normalizeHtml(range.getValidHtml()));
<a name="line195"></a>  assertFalse(&#39;Div range is not collapsed&#39;, range.isCollapsed());
<a name="line196"></a>}
<a name="line197"></a>
<a name="line198"></a>function testEmptyNodeHtmlInsert() {
<a name="line199"></a>  var range = goog.dom.browserrange.createRangeFromNodeContents(empty);
<a name="line200"></a>  var html = &#39;&lt;b&gt;hello&lt;/b&gt;&#39;;
<a name="line201"></a>  range.insertNode(goog.dom.htmlToDocumentFragment(html));
<a name="line202"></a>  assertEquals(&#39;Html is not inserted correctly&#39;, html,
<a name="line203"></a>      normalizeHtml(empty.innerHTML));
<a name="line204"></a>  goog.dom.removeChildren(empty);
<a name="line205"></a>}
<a name="line206"></a>
<a name="line207"></a>function testEmptyNode() {
<a name="line208"></a>  var range = goog.dom.browserrange.createRangeFromNodeContents(empty);
<a name="line209"></a>
<a name="line210"></a>  assertEquals(&#39;DIV be selected at start endpoint&#39;, &#39;DIV&#39;,
<a name="line211"></a>      range.getStartNode().tagName);
<a name="line212"></a>  assertEquals(&#39;Selection should start at offset 0&#39;, 0,
<a name="line213"></a>      range.getStartOffset());
<a name="line214"></a>
<a name="line215"></a>  assertEquals(&#39;DIV should be selected at end endpoint&#39;, &#39;DIV&#39;,
<a name="line216"></a>      range.getEndNode().tagName);
<a name="line217"></a>  assertEquals(&#39;Selection should end at offset 0&#39;, 0,
<a name="line218"></a>      range.getEndOffset());
<a name="line219"></a>
<a name="line220"></a>  assertEquals(&#39;Container should be DIV&#39;, &#39;DIV&#39;,
<a name="line221"></a>      range.getContainer().tagName);
<a name="line222"></a>
<a name="line223"></a>  assertEquals(&#39;Empty text content should be &quot;&quot;&#39;, &#39;&#39;, range.getText());
<a name="line224"></a>  assertTrue(&#39;Empty range is collapsed&#39;, range.isCollapsed());
<a name="line225"></a>  assertEquals(&#39;Should contain correct valid html&#39;, &#39;&lt;div id=empty&gt;&lt;/div&gt;&#39;,
<a name="line226"></a>      normalizeHtml(range.getValidHtml()));
<a name="line227"></a>  assertEquals(&#39;Should contain no html fragment&#39;, &#39;&#39;,
<a name="line228"></a>      range.getHtmlFragment());
<a name="line229"></a>}
<a name="line230"></a>
<a name="line231"></a>
<a name="line232"></a>function testRemoveContents() {
<a name="line233"></a>  var outer = goog.dom.getElement(&#39;removeTest&#39;);
<a name="line234"></a>  var range = goog.dom.browserrange.createRangeFromNodeContents(
<a name="line235"></a>      outer.firstChild);
<a name="line236"></a>
<a name="line237"></a>  range.removeContents();
<a name="line238"></a>
<a name="line239"></a>  assertEquals(&#39;Removed range content should be &quot;&quot;&#39;, &#39;&#39;, range.getText());
<a name="line240"></a>  assertTrue(&#39;Removed range is now collapsed&#39;, range.isCollapsed());
<a name="line241"></a>  assertEquals(&#39;Outer div has 1 child now&#39;, 1, outer.childNodes.length);
<a name="line242"></a>  assertEquals(&#39;Inner div is empty&#39;, 0, outer.firstChild.childNodes.length);
<a name="line243"></a>}
<a name="line244"></a>
<a name="line245"></a>
<a name="line246"></a>function testRemoveContentsEmptyNode() {
<a name="line247"></a>  var outer = goog.dom.getElement(&#39;removeTestEmptyNode&#39;);
<a name="line248"></a>  var range = goog.dom.browserrange.createRangeFromNodeContents(
<a name="line249"></a>      outer);
<a name="line250"></a>
<a name="line251"></a>  range.removeContents();
<a name="line252"></a>
<a name="line253"></a>  assertEquals(&#39;Removed range content should be &quot;&quot;&#39;, &#39;&#39;, range.getText());
<a name="line254"></a>  assertTrue(&#39;Removed range is now collapsed&#39;, range.isCollapsed());
<a name="line255"></a>  assertEquals(&#39;Outer div should have 0 children now&#39;,
<a name="line256"></a>      0, outer.childNodes.length);
<a name="line257"></a>}
<a name="line258"></a>
<a name="line259"></a>
<a name="line260"></a>function testRemoveContentsSingleNode() {
<a name="line261"></a>  var outer = goog.dom.getElement(&#39;removeTestSingleNode&#39;);
<a name="line262"></a>  var range = goog.dom.browserrange.createRangeFromNodeContents(
<a name="line263"></a>      outer.firstChild);
<a name="line264"></a>
<a name="line265"></a>  range.removeContents();
<a name="line266"></a>
<a name="line267"></a>  assertEquals(&#39;Removed range content should be &quot;&quot;&#39;, &#39;&#39;, range.getText());
<a name="line268"></a>  assertTrue(&#39;Removed range is now collapsed&#39;, range.isCollapsed());
<a name="line269"></a>  assertEquals(&#39;&#39;, goog.dom.getTextContent(outer));
<a name="line270"></a>}
<a name="line271"></a>
<a name="line272"></a>
<a name="line273"></a>function testRemoveContentsMidNode() {
<a name="line274"></a>  var outer = goog.dom.getElement(&#39;removeTestMidNode&#39;);
<a name="line275"></a>  var textNode = outer.firstChild.firstChild;
<a name="line276"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line277"></a>      textNode, 1, textNode, 4);
<a name="line278"></a>
<a name="line279"></a>  assertEquals(&#39;Previous range content should be &quot;123&quot;&#39;, &#39;123&#39;,
<a name="line280"></a>      range.getText());
<a name="line281"></a>  range.removeContents();
<a name="line282"></a>
<a name="line283"></a>  assertEquals(&#39;Removed range content should be &quot;0456789&quot;&#39;, &#39;0456789&#39;,
<a name="line284"></a>      goog.dom.getTextContent(outer));
<a name="line285"></a>}
<a name="line286"></a>
<a name="line287"></a>
<a name="line288"></a>function testRemoveContentsMidMultipleNodes() {
<a name="line289"></a>  var outer = goog.dom.getElement(&#39;removeTestMidMultipleNodes&#39;);
<a name="line290"></a>  var firstTextNode = outer.firstChild.firstChild;
<a name="line291"></a>  var lastTextNode = outer.lastChild.firstChild;
<a name="line292"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line293"></a>      firstTextNode, 1, lastTextNode, 4);
<a name="line294"></a>
<a name="line295"></a>  assertEquals(&#39;Previous range content&#39;, &#39;1234567890123&#39;,
<a name="line296"></a>      range.getText().replace(/\s/g, &#39;&#39;));
<a name="line297"></a>  range.removeContents();
<a name="line298"></a>
<a name="line299"></a>  assertEquals(&#39;Removed range content should be &quot;0456789&quot;&#39;, &#39;0456789&#39;,
<a name="line300"></a>      goog.dom.getTextContent(outer).replace(/\s/g, &#39;&#39;));
<a name="line301"></a>}
<a name="line302"></a>
<a name="line303"></a>
<a name="line304"></a>function testRemoveDivCaretRange() {
<a name="line305"></a>  var outer = goog.dom.getElement(&#39;sandbox&#39;);
<a name="line306"></a>  outer.innerHTML = &#39;&lt;div&gt;Test1&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&#39;;
<a name="line307"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line308"></a>      outer.lastChild, 0, outer.lastChild, 0);
<a name="line309"></a>
<a name="line310"></a>  range.removeContents();
<a name="line311"></a>  range.insertNode(goog.dom.createDom(&#39;span&#39;, undefined, &#39;Hello&#39;), true);
<a name="line312"></a>
<a name="line313"></a>  assertEquals(&#39;Resulting contents&#39;, &#39;Test1Hello&#39;,
<a name="line314"></a>      goog.dom.getTextContent(outer).replace(/\s/g, &#39;&#39;));
<a name="line315"></a>}
<a name="line316"></a>
<a name="line317"></a>
<a name="line318"></a>function testCollapse() {
<a name="line319"></a>  var range = goog.dom.browserrange.createRangeFromNodeContents(test2);
<a name="line320"></a>  assertFalse(&#39;Div range is not collapsed&#39;, range.isCollapsed());
<a name="line321"></a>  range.collapse();
<a name="line322"></a>  assertTrue(&#39;Div range is collapsed after call to empty()&#39;,
<a name="line323"></a>      range.isCollapsed());
<a name="line324"></a>
<a name="line325"></a>  range = goog.dom.browserrange.createRangeFromNodeContents(empty);
<a name="line326"></a>  assertTrue(&#39;Empty range is collapsed&#39;, range.isCollapsed());
<a name="line327"></a>  range.collapse();
<a name="line328"></a>  assertTrue(&#39;Empty range is still collapsed&#39;, range.isCollapsed());
<a name="line329"></a>}
<a name="line330"></a>
<a name="line331"></a>
<a name="line332"></a>function testIdWithSpecialCharacters() {
<a name="line333"></a>  goog.dom.removeChildren(dynamic);
<a name="line334"></a>  dynamic.appendChild(goog.dom.createTextNode(&#39;1&#39;));
<a name="line335"></a>  dynamic.appendChild(goog.dom.createDom(&#39;div&#39;, {id: &#39;&lt;&gt;&#39;}));
<a name="line336"></a>  dynamic.appendChild(goog.dom.createTextNode(&#39;2&#39;));
<a name="line337"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line338"></a>      dynamic.firstChild, 0, dynamic.lastChild, 1);
<a name="line339"></a>
<a name="line340"></a>  // Difference in special character handling is ok.
<a name="line341"></a>  assertContains(&#39;Should have correct html fragment&#39;,
<a name="line342"></a>      normalizeHtml(range.getHtmlFragment()),
<a name="line343"></a>      [
<a name="line344"></a>        &#39;1&lt;div id=&lt;&gt;&gt;&lt;/div&gt;2&#39;, // IE
<a name="line345"></a>        &#39;1&lt;div id=&amp;lt;&gt;&gt;&lt;/div&gt;2&#39;, // WebKit
<a name="line346"></a>        &#39;1&lt;div id=&amp;lt;&amp;gt;&gt;&lt;/div&gt;2&#39; // Others
<a name="line347"></a>      ]);
<a name="line348"></a>}
<a name="line349"></a>
<a name="line350"></a>function testEndOfChildren() {
<a name="line351"></a>  dynamic.innerHTML =
<a name="line352"></a>      &#39;&lt;span id=&quot;a&quot;&gt;123&lt;br&gt;456&lt;/span&gt;&lt;span id=&quot;b&quot;&gt;text&lt;/span&gt;&#39;;
<a name="line353"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line354"></a>      goog.dom.getElement(&#39;a&#39;), 3, goog.dom.getElement(&#39;b&#39;), 1);
<a name="line355"></a>  assertEquals(&#39;Should have correct text.&#39;, &#39;text&#39;, range.getText());
<a name="line356"></a>}
<a name="line357"></a>
<a name="line358"></a>function testEndOfDiv() {
<a name="line359"></a>  dynamic.innerHTML = &#39;&lt;div id=&quot;a&quot;&gt;abc&lt;/div&gt;&lt;div id=&quot;b&quot;&gt;def&lt;/div&gt;&#39;;
<a name="line360"></a>  var a = goog.dom.getElement(&#39;a&#39;);
<a name="line361"></a>  var range = goog.dom.browserrange.createRangeFromNodes(a, 1, a, 1);
<a name="line362"></a>  var expectedStartNode = a;
<a name="line363"></a>  var expectedStartOffset = 1;
<a name="line364"></a>  var expectedEndNode = a;
<a name="line365"></a>  var expectedEndOffset = 1;
<a name="line366"></a>  assertEquals(&#39;startNode is wrong&#39;, expectedStartNode, range.getStartNode());
<a name="line367"></a>  assertEquals(&#39;startOffset is wrong&#39;,
<a name="line368"></a>      expectedStartOffset, range.getStartOffset());
<a name="line369"></a>  assertEquals(&#39;endNode is wrong&#39;, expectedEndNode, range.getEndNode());
<a name="line370"></a>  assertEquals(&#39;endOffset is wrong&#39;, expectedEndOffset, range.getEndOffset());
<a name="line371"></a>}
<a name="line372"></a>
<a name="line373"></a>function testRangeEndingWithBR() {
<a name="line374"></a>  dynamic.innerHTML = &#39;&lt;span id=&quot;a&quot;&gt;123&lt;br&gt;456&lt;/span&gt;&#39;;
<a name="line375"></a>  var spanElem = goog.dom.getElement(&#39;a&#39;);
<a name="line376"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line377"></a>      spanElem, 0, spanElem, 2);
<a name="line378"></a>  var htmlText = range.getValidHtml().toLowerCase();
<a name="line379"></a>  assertContains(&#39;Should include BR in HTML.&#39;, &#39;br&#39;, htmlText);
<a name="line380"></a>  assertEquals(&#39;Should have correct text.&#39;, &#39;123&#39;, range.getText());
<a name="line381"></a>
<a name="line382"></a>  range.select();
<a name="line383"></a>
<a name="line384"></a>  var selRange = goog.dom.Range.createFromWindow();
<a name="line385"></a>  var startNode = selRange.getStartNode();
<a name="line386"></a>  if (goog.userAgent.GECKO ||
<a name="line387"></a>      (goog.userAgent.IE &amp;&amp; goog.userAgent.isDocumentModeOrHigher(9))) {
<a name="line388"></a>    assertEquals(&#39;Start node should be span&#39;, spanElem, startNode);
<a name="line389"></a>  } else {
<a name="line390"></a>    assertEquals(&#39;Startnode should have text:123&#39;,
<a name="line391"></a>        &#39;123&#39;, startNode.nodeValue);
<a name="line392"></a>  }
<a name="line393"></a>  assertEquals(&#39;Startoffset should be 0&#39;, 0, selRange.getStartOffset());
<a name="line394"></a>  var endNode = selRange.getEndNode();
<a name="line395"></a>  assertEquals(&#39;Endnode should be span&#39;, spanElem, endNode);
<a name="line396"></a>  assertEquals(&#39;Endoffset should be 2&#39;, 2, selRange.getEndOffset());
<a name="line397"></a>}
<a name="line398"></a>
<a name="line399"></a>function testRangeEndingWithBR2() {
<a name="line400"></a>  dynamic.innerHTML = &#39;&lt;span id=&quot;a&quot;&gt;123&lt;br&gt;&lt;/span&gt;&#39;;
<a name="line401"></a>  var spanElem = goog.dom.getElement(&#39;a&#39;);
<a name="line402"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line403"></a>      spanElem, 0, spanElem, 2);
<a name="line404"></a>  var htmlText = range.getValidHtml().toLowerCase();
<a name="line405"></a>  assertContains(&#39;Should include BR in HTML.&#39;, &#39;br&#39;, htmlText);
<a name="line406"></a>  assertEquals(&#39;Should have correct text.&#39;, &#39;123&#39;, range.getText());
<a name="line407"></a>
<a name="line408"></a>  range.select();
<a name="line409"></a>
<a name="line410"></a>  var selRange = goog.dom.Range.createFromWindow();
<a name="line411"></a>  var startNode = selRange.getStartNode();
<a name="line412"></a>  if (goog.userAgent.GECKO ||
<a name="line413"></a>      (goog.userAgent.IE &amp;&amp; goog.userAgent.isDocumentModeOrHigher(9))) {
<a name="line414"></a>    assertEquals(&#39;Start node should be span&#39;, spanElem, startNode);
<a name="line415"></a>  } else {
<a name="line416"></a>    assertEquals(&#39;Start node should have text:123&#39;,
<a name="line417"></a>        &#39;123&#39;, startNode.nodeValue);
<a name="line418"></a>  }
<a name="line419"></a>  assertEquals(&#39;Startoffset should be 0&#39;, 0, selRange.getStartOffset());
<a name="line420"></a>  var endNode = selRange.getEndNode();
<a name="line421"></a>  if (goog.userAgent.WEBKIT) {
<a name="line422"></a>    assertEquals(&#39;Endnode should have text&#39;, &#39;123&#39;, endNode.nodeValue);
<a name="line423"></a>    assertEquals(&#39;Endoffset should be 3&#39;, 3, selRange.getEndOffset());
<a name="line424"></a>  } else {
<a name="line425"></a>    assertEquals(&#39;Endnode should be span&#39;, spanElem, endNode);
<a name="line426"></a>    assertEquals(&#39;Endoffset should be 2&#39;, 2, selRange.getEndOffset());
<a name="line427"></a>  }
<a name="line428"></a>}
<a name="line429"></a>
<a name="line430"></a>function testRangeEndingBeforeBR() {
<a name="line431"></a>  dynamic.innerHTML = &#39;&lt;span id=&quot;a&quot;&gt;123&lt;br&gt;456&lt;/span&gt;&#39;;
<a name="line432"></a>  var spanElem = goog.dom.getElement(&#39;a&#39;);
<a name="line433"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line434"></a>      spanElem, 0, spanElem, 1);
<a name="line435"></a>  var htmlText = range.getValidHtml().toLowerCase();
<a name="line436"></a>  assertNotContains(&#39;Should not include BR in HTML.&#39;, &#39;br&#39;, htmlText);
<a name="line437"></a>  assertEquals(&#39;Should have correct text.&#39;, &#39;123&#39;, range.getText());
<a name="line438"></a>  range.select();
<a name="line439"></a>
<a name="line440"></a>  var selRange = goog.dom.Range.createFromWindow();
<a name="line441"></a>  var startNode = selRange.getStartNode();
<a name="line442"></a>  if (goog.userAgent.GECKO ||
<a name="line443"></a>      (goog.userAgent.IE &amp;&amp; goog.userAgent.isDocumentModeOrHigher(9))) {
<a name="line444"></a>    assertEquals(&#39;Start node should be span&#39;, spanElem, startNode);
<a name="line445"></a>  } else {
<a name="line446"></a>    assertEquals(&#39;Startnode should have text:123&#39;,
<a name="line447"></a>        &#39;123&#39;, startNode.nodeValue);
<a name="line448"></a>  }
<a name="line449"></a>  assertEquals(&#39;Startoffset should be 0&#39;, 0, selRange.getStartOffset());
<a name="line450"></a>  var endNode = selRange.getEndNode();
<a name="line451"></a>  if (goog.userAgent.GECKO ||
<a name="line452"></a>      (goog.userAgent.IE &amp;&amp; goog.userAgent.isDocumentModeOrHigher(9))) {
<a name="line453"></a>    assertEquals(&#39;Endnode should be span&#39;, spanElem, endNode);
<a name="line454"></a>    assertEquals(&#39;Endoffset should be 1&#39;, 1, selRange.getEndOffset());
<a name="line455"></a>  } else {
<a name="line456"></a>    assertEquals(&#39;Endnode should have text:123&#39;, &#39;123&#39;, endNode.nodeValue);
<a name="line457"></a>    assertEquals(&#39;Endoffset should be 3&#39;, 3, selRange.getEndOffset());
<a name="line458"></a>  }
<a name="line459"></a>}
<a name="line460"></a>
<a name="line461"></a>function testRangeStartingWithBR() {
<a name="line462"></a>  dynamic.innerHTML = &#39;&lt;span id=&quot;a&quot;&gt;123&lt;br&gt;456&lt;/span&gt;&#39;;
<a name="line463"></a>  var spanElem = goog.dom.getElement(&#39;a&#39;);
<a name="line464"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line465"></a>      spanElem, 1, spanElem, 3);
<a name="line466"></a>  var htmlText = range.getValidHtml().toLowerCase();
<a name="line467"></a>  assertContains(&#39;Should include BR in HTML.&#39;, &#39;br&#39;, htmlText);
<a name="line468"></a>  // Firefox returns &#39;456&#39; as the range text while IE returns &#39;\r\n456&#39;.
<a name="line469"></a>  // Therefore skipping the text check.
<a name="line470"></a>
<a name="line471"></a>  range.select();
<a name="line472"></a>  var selRange = goog.dom.Range.createFromWindow();
<a name="line473"></a>  var startNode = selRange.getStartNode();
<a name="line474"></a>  assertEquals(&#39;Start node should be span&#39;, spanElem, startNode);
<a name="line475"></a>  assertEquals(&#39;Startoffset should be 1&#39;, 1, selRange.getStartOffset());
<a name="line476"></a>  var endNode = selRange.getEndNode();
<a name="line477"></a>  if (goog.userAgent.GECKO ||
<a name="line478"></a>      (goog.userAgent.IE &amp;&amp; goog.userAgent.isDocumentModeOrHigher(9))) {
<a name="line479"></a>    assertEquals(&#39;Endnode should be span&#39;, spanElem, endNode);
<a name="line480"></a>    assertEquals(&#39;Endoffset should be 3&#39;, 3, selRange.getEndOffset());
<a name="line481"></a>  } else {
<a name="line482"></a>    assertEquals(&#39;Endnode should have text:456&#39;, &#39;456&#39;, endNode.nodeValue);
<a name="line483"></a>    assertEquals(&#39;Endoffset should be 3&#39;, 3, selRange.getEndOffset());
<a name="line484"></a>  }
<a name="line485"></a>}
<a name="line486"></a>
<a name="line487"></a>function testRangeStartingAfterBR() {
<a name="line488"></a>  dynamic.innerHTML = &#39;&lt;span id=&quot;a&quot;&gt;123&lt;br&gt;4567&lt;/span&gt;&#39;;
<a name="line489"></a>  var spanElem = goog.dom.getElement(&#39;a&#39;);
<a name="line490"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line491"></a>      spanElem, 2, spanElem, 3);
<a name="line492"></a>  var htmlText = range.getValidHtml().toLowerCase();
<a name="line493"></a>  assertNotContains(&#39;Should not include BR in HTML.&#39;, &#39;br&#39;, htmlText);
<a name="line494"></a>  assertEquals(&#39;Should have correct text.&#39;, &#39;4567&#39;, range.getText());
<a name="line495"></a>
<a name="line496"></a>  range.select();
<a name="line497"></a>
<a name="line498"></a>  var selRange = goog.dom.Range.createFromWindow();
<a name="line499"></a>  var startNode = selRange.getStartNode();
<a name="line500"></a>  if (goog.userAgent.GECKO ||
<a name="line501"></a>      (goog.userAgent.IE &amp;&amp; goog.userAgent.isDocumentModeOrHigher(9))) {
<a name="line502"></a>    assertEquals(&#39;Start node should be span&#39;, spanElem, startNode);
<a name="line503"></a>    assertEquals(&#39;Startoffset should be 2&#39;, 2, selRange.getStartOffset());
<a name="line504"></a>  } else {
<a name="line505"></a>    assertEquals(&#39;Startnode should have text:4567&#39;,
<a name="line506"></a>        &#39;4567&#39;, startNode.nodeValue);
<a name="line507"></a>    assertEquals(&#39;Startoffset should be 0&#39;, 0, selRange.getStartOffset());
<a name="line508"></a>  }
<a name="line509"></a>  var endNode = selRange.getEndNode();
<a name="line510"></a>  if (goog.userAgent.GECKO ||
<a name="line511"></a>      (goog.userAgent.IE &amp;&amp; goog.userAgent.isDocumentModeOrHigher(9))) {
<a name="line512"></a>    assertEquals(&#39;Endnode should be span&#39;, spanElem, endNode);
<a name="line513"></a>    assertEquals(&#39;Endoffset should be 3&#39;, 3, selRange.getEndOffset());
<a name="line514"></a>  } else {
<a name="line515"></a>    assertEquals(&#39;Endnode should have text:4567&#39;, &#39;4567&#39;, endNode.nodeValue);
<a name="line516"></a>    assertEquals(&#39;Endoffset should be 4&#39;, 4, selRange.getEndOffset());
<a name="line517"></a>  }
<a name="line518"></a>
<a name="line519"></a>}
<a name="line520"></a>
<a name="line521"></a>function testCollapsedRangeBeforeBR() {
<a name="line522"></a>  dynamic.innerHTML = &#39;&lt;span id=&quot;a&quot;&gt;123&lt;br&gt;456&lt;/span&gt;&#39;;
<a name="line523"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line524"></a>      goog.dom.getElement(&#39;a&#39;), 1, goog.dom.getElement(&#39;a&#39;), 1);
<a name="line525"></a>  // Firefox returns &lt;span id=&quot;a&quot;&gt;&lt;/span&gt; as the range HTML while IE returns
<a name="line526"></a>  // empty string. Therefore skipping the HTML check.
<a name="line527"></a>  assertEquals(&#39;Should have no text.&#39;, &#39;&#39;, range.getText());
<a name="line528"></a>}
<a name="line529"></a>
<a name="line530"></a>function testCollapsedRangeAfterBR() {
<a name="line531"></a>  dynamic.innerHTML = &#39;&lt;span id=&quot;a&quot;&gt;123&lt;br&gt;456&lt;/span&gt;&#39;;
<a name="line532"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line533"></a>      goog.dom.getElement(&#39;a&#39;), 2, goog.dom.getElement(&#39;a&#39;), 2);
<a name="line534"></a>  // Firefox returns &lt;span id=&quot;a&quot;&gt;&lt;/span&gt; as the range HTML while IE returns
<a name="line535"></a>  // empty string. Therefore skipping the HTML check.
<a name="line536"></a>  assertEquals(&#39;Should have no text.&#39;, &#39;&#39;, range.getText());
<a name="line537"></a>}
<a name="line538"></a>
<a name="line539"></a>function testCompareBrowserRangeEndpoints() {
<a name="line540"></a>  var outer = goog.dom.getElement(&#39;outer&#39;);
<a name="line541"></a>  var inner = goog.dom.getElement(&#39;inner&#39;);
<a name="line542"></a>  var range_outer = goog.dom.browserrange.createRangeFromNodeContents(outer);
<a name="line543"></a>  var range_inner = goog.dom.browserrange.createRangeFromNodeContents(inner);
<a name="line544"></a>
<a name="line545"></a>  assertEquals(
<a name="line546"></a>      &#39;The start of the inner selection should be after the outer.&#39;,
<a name="line547"></a>      1,
<a name="line548"></a>      range_inner.compareBrowserRangeEndpoints(
<a name="line549"></a>          range_outer.getBrowserRange(),
<a name="line550"></a>          goog.dom.RangeEndpoint.START,
<a name="line551"></a>          goog.dom.RangeEndpoint.START));
<a name="line552"></a>
<a name="line553"></a>  assertEquals(
<a name="line554"></a>      &quot;The start of the inner selection should be before the outer&#39;s end.&quot;,
<a name="line555"></a>      -1,
<a name="line556"></a>      range_inner.compareBrowserRangeEndpoints(
<a name="line557"></a>          range_outer.getBrowserRange(),
<a name="line558"></a>          goog.dom.RangeEndpoint.START,
<a name="line559"></a>          goog.dom.RangeEndpoint.END));
<a name="line560"></a>
<a name="line561"></a>  assertEquals(
<a name="line562"></a>      &quot;The end of the inner selection should be after the outer&#39;s start.&quot;,
<a name="line563"></a>      1,
<a name="line564"></a>      range_inner.compareBrowserRangeEndpoints(
<a name="line565"></a>          range_outer.getBrowserRange(),
<a name="line566"></a>          goog.dom.RangeEndpoint.END,
<a name="line567"></a>          goog.dom.RangeEndpoint.START));
<a name="line568"></a>
<a name="line569"></a>  assertEquals(
<a name="line570"></a>      &quot;The end of the inner selection should be before the outer&#39;s end.&quot;,
<a name="line571"></a>      -1,
<a name="line572"></a>      range_inner.compareBrowserRangeEndpoints(
<a name="line573"></a>          range_outer.getBrowserRange(),
<a name="line574"></a>          goog.dom.RangeEndpoint.END,
<a name="line575"></a>          goog.dom.RangeEndpoint.END));
<a name="line576"></a>
<a name="line577"></a>}
<a name="line578"></a>
<a name="line579"></a>
<a name="line580"></a>/**
<a name="line581"></a> * Regression test for a bug in IeRange.insertNode_ where if the node to be
<a name="line582"></a> * inserted was not an element (e.g. a text node), it would clone the node
<a name="line583"></a> * in the inserting process but return the original node instead of the newly
<a name="line584"></a> * created and inserted node.
<a name="line585"></a> */
<a name="line586"></a>function testInsertNodeNonElement() {
<a name="line587"></a>  dynamic.innerHTML = &#39;beforeafter&#39;;
<a name="line588"></a>  var range = goog.dom.browserrange.createRangeFromNodes(
<a name="line589"></a>      dynamic.firstChild, 6, dynamic.firstChild, 6);
<a name="line590"></a>  var newNode = goog.dom.createTextNode(&#39;INSERTED&#39;);
<a name="line591"></a>  var inserted = range.insertNode(newNode, false);
<a name="line592"></a>
<a name="line593"></a>  assertEquals(&#39;Text should be inserted between &quot;before&quot; and &quot;after&quot;&#39;,
<a name="line594"></a>               &#39;beforeINSERTEDafter&#39;,
<a name="line595"></a>               goog.dom.getRawTextContent(dynamic));
<a name="line596"></a>  assertEquals(&#39;Node returned by insertNode() should be a child of the div&#39; +
<a name="line597"></a>               &#39; containing the text&#39;,
<a name="line598"></a>               dynamic,
<a name="line599"></a>               inserted.parentNode);
<a name="line600"></a>}
<a name="line601"></a>
<a name="line602"></a>function testSelectOverwritesOldSelection() {
<a name="line603"></a>  goog.dom.browserrange.createRangeFromNodes(test1, 0, test1, 1).select();
<a name="line604"></a>  goog.dom.browserrange.createRangeFromNodes(test2, 0, test2, 1).select();
<a name="line605"></a>  assertEquals(&#39;The old selection must be replaced with the new one&#39;,
<a name="line606"></a>               &#39;abc&#39;, goog.dom.Range.createFromWindow().getText());
<a name="line607"></a>}
<a name="line608"></a>
<a name="line609"></a>// Following testcase is special for IE. The comparison of ranges created in
<a name="line610"></a>// testcases with a range over empty span using native inRange fails. So the
<a name="line611"></a>// fallback mechanism is needed.
<a name="line612"></a>function testGetContainerInTextNodesAroundEmptySpan() {
<a name="line613"></a>  dynamic.innerHTML = &#39;abc&lt;span&gt;&lt;/span&gt;def&#39;;
<a name="line614"></a>  var abc = dynamic.firstChild;
<a name="line615"></a>  var def = dynamic.lastChild;
<a name="line616"></a>
<a name="line617"></a>  var range;
<a name="line618"></a>  range = goog.dom.browserrange.createRangeFromNodes(abc, 1, abc, 1);
<a name="line619"></a>  assertEquals(&#39;textNode abc should be the range container&#39;,
<a name="line620"></a>      abc, range.getContainer());
<a name="line621"></a>  assertEquals(&#39;textNode abc should be the range start node&#39;,
<a name="line622"></a>      abc, range.getStartNode());
<a name="line623"></a>  assertEquals(&#39;textNode abc should be the range end node&#39;,
<a name="line624"></a>      abc, range.getEndNode());
<a name="line625"></a>
<a name="line626"></a>  range = goog.dom.browserrange.createRangeFromNodes(def, 1, def, 1);
<a name="line627"></a>  assertEquals(&#39;textNode def should be the range container&#39;,
<a name="line628"></a>      def, range.getContainer());
<a name="line629"></a>  assertEquals(&#39;textNode def should be the range start node&#39;,
<a name="line630"></a>      def, range.getStartNode());
<a name="line631"></a>  assertEquals(&#39;textNode def should be the range end node&#39;,
<a name="line632"></a>      def, range.getEndNode());
<a name="line633"></a>}
</pre>


</body>
</html>
